package game.ability;

import game.entity.SpriteManager;
import game.shared.Constants;

import java.util.Random;
import java.util.logging.FileHandler;

import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class Lightning  extends AbsAbility{

	ShapeRenderer shapeRenderer;
	Camera camera;
	Rectangle viewport;
	SpriteManager monsterManager;
	Vector2 endPoint;
	int iSelectID;
	int curDetail;
	int counts;
	Random r;
	int speed;
	//Sound s;

	public Lightning(SpriteManager monsterManager, Camera camera, Rectangle viewport, ShapeRenderer shapeRenderer) {
		super();
		this.monsterManager = monsterManager;
//		this.camera = camera;
//		this.viewport = viewport;
		this.shapeRenderer = shapeRenderer;
		shapeRenderer.setColor(144, 254, 115, 1);
		endPoint = new Vector2();
		curDetail = 3;
		iSelectID = 3;
		counts = 0;
		speed = 12;
		r = new Random();
		//s = Gdx.audio.newSound(Gdx.files.internal("res/sound/achievement.ogg"));
		//s = Gdx.audio.newSound(Gdx.files.internal("res/sound/electricshock.wav"));
	}

	@Override
	public void renderShape() {
		if (Gdx.input.isTouched()){
			for(int i=0; i<monsterManager.count(); i++){
				if(monsterManager.getByIndex(i).getBoundingRectangle().overlaps(
						new Rectangle(Gdx.input.getX()-10, Constants.SCREEN_HEIGHT - Gdx.input.getY()-10, 20, 20)))
					iSelectID = monsterManager.getByIndex(i).getID();
			}
		}
		
		if(monsterManager.getByID(iSelectID) != null){
			endPoint.set(monsterManager.getByID(iSelectID).getX() - Constants.SCREEN_WIDTH / 2 + monsterManager.getByID(iSelectID).getWidth() / 3, 
					monsterManager.getByID(iSelectID).getY() - Constants.SCREEN_HEIGHT / 2 + monsterManager.getByID(iSelectID).getHeight() / 2);
			shapeRenderer.begin(ShapeType.Line);
			
			Gdx.gl10.glLineWidth(2);
			drawLightning(-179, 66, endPoint.x, endPoint.y, 70);
			drawLightning(-179, 66, endPoint.x, endPoint.y, 70);

			counts ++;
			if (counts > speed){
				//s.play(0.1f);
				monsterManager.getByID(iSelectID).setHp((float)(monsterManager.getByID(iSelectID).getHp() - Constants.DAM));
				if (counts > 15)
					counts = 0;
			}
//			if(monsterManager.getByID(iSelectID).getHp() <= 0)
//				monsterManager.removeById(iSelectID);
			shapeRenderer.end();
			
		}
	}

	public void drawLightning(float x1, float y1, float x2, float y2, float display) {
		if (display < curDetail) {
			shapeRenderer.line(x1, y1, x2, y2);
		} else {
			float mid_x = (x2 + x1) / 2;
			float mid_y = (y2 + y1) / 2;
			mid_x += (r.nextFloat() - 0.3) * display;
			mid_y += (r.nextFloat() - 0.3) * display;
			drawLightning(x1, y1, mid_x, mid_y, display / 2);
			drawLightning(x2, y2, mid_x, mid_y, display / 2);
		}
	}
}